package cn.lngaosu.lwyd.module.integration.controller.admin.weixinarticle;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.integration.controller.admin.weixinarticle.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weixinarticle.WeixinArticleDO;
import cn.lngaosu.lwyd.module.integration.service.weixinarticle.WeixinArticleService;

@Tag(name = "管理后台 - 微信文章")
@RestController
@RequestMapping("/integration/weixin-article")
@Validated
public class WeixinArticleController {

    @Resource
    private WeixinArticleService weixinArticleService;

    @PostMapping("/create")
    @Operation(summary = "创建微信文章")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article:create')")
    public CommonResult<Long> createWeixinArticle(@Valid @RequestBody WeixinArticleSaveReqVO createReqVO) {
        return success(weixinArticleService.createWeixinArticle(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新微信文章")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article:update')")
    public CommonResult<Boolean> updateWeixinArticle(@Valid @RequestBody WeixinArticleSaveReqVO updateReqVO) {
        weixinArticleService.updateWeixinArticle(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除微信文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('integration:weixin-article:delete')")
    public CommonResult<Boolean> deleteWeixinArticle(@RequestParam("id") Long id) {
        weixinArticleService.deleteWeixinArticle(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微信文章")
                @PreAuthorize("@ss.hasPermission('integration:weixin-article:delete')")
    public CommonResult<Boolean> deleteWeixinArticleList(@RequestParam("ids") List<Long> ids) {
        weixinArticleService.deleteWeixinArticleListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article:query')")
    public CommonResult<WeixinArticleRespVO> getWeixinArticle(@RequestParam("id") Long id) {
        WeixinArticleDO weixinArticle = weixinArticleService.getWeixinArticle(id);
        return success(BeanUtils.toBean(weixinArticle, WeixinArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信文章分页")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article:query')")
    public CommonResult<PageResult<WeixinArticleRespVO>> getWeixinArticlePage(@Valid WeixinArticlePageReqVO pageReqVO) {
        PageResult<WeixinArticleDO> pageResult = weixinArticleService.getWeixinArticlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeixinArticleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信文章 Excel")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeixinArticleExcel(@Valid WeixinArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeixinArticleDO> list = weixinArticleService.getWeixinArticlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信文章.xls", "数据", WeixinArticleRespVO.class,
                        BeanUtils.toBean(list, WeixinArticleRespVO.class));
    }

}